home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / Matlab mode / matlabComm.tcl next >
Encoding:
Text File  |  2000-05-10  |  11.0 KB  |  413 lines

  1. ################################################################################
  2. #
  3. # matlabComm.tcl, part of the matlab mode package
  4. # Communicate with MATLAB
  5. ################################################################################
  6.  
  7. proc matlabComm.tcl {} {}
  8.  
  9. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
  10. #
  11. # ◊◊◊◊ Core interaction with MATLAB ◊◊◊◊ #
  12. #
  13. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-
  14.  
  15. #################################################################################
  16. # Launch MATLAB in background if not already running
  17. ################################################################################
  18.  
  19. proc matlabLaunch {} {
  20.     global matlabSig
  21.     set name [app::launchBack $matlabSig]
  22.  
  23. #     global matlabPath
  24. #     catch {checkRunning MATLAB MATL matlabPath} name
  25.  
  26.     return $name
  27. }
  28.  
  29.  
  30. #################################################################################
  31. # Tell MATLAB to do a script.  
  32. # Echo script and script results in command window if desired.
  33. #   showResults = 0 show nothing
  34. #                 1 echo command in command window and show results when done
  35. #                 2 do not echo command but do show results when done
  36. #   forceWait = 0 que command 
  37. #               1 wait for result and return it
  38. #               
  39. #  If sending a list of scripts all argins must be present and of same length 
  40. #  except forceWait which can only be a single 0
  41. ################################################################################
  42.  
  43. proc matlabDoScript {scriptName {showResults 1} {forceWait 0} {replyHandler MATL::ReplyHandler}} {
  44.     global MATLmodeVars MATLeventQue matlabBusy
  45.     
  46.     matlabLaunch
  47.     
  48.     if {$forceWait} {
  49.         message "Waiting for result from MATLAB"
  50.         set res [dosc -c {'MATL'} -s $scriptName]
  51.         message "Got result from MATLAB"
  52.         if {$showResults} {
  53.             matlabCmdWindow
  54.             insertText "${scriptName}\n"
  55.             matlabResults $res
  56.         }
  57.         return $res
  58.     }
  59.     
  60.     if {$matlabBusy && ! $MATLmodeVars(queEventsQuietly)} {
  61.         if {[askyesno "Matlab is busy, should I queue your request:\r\"$scriptName\""] == "no"} {
  62.             return
  63.         }
  64.     }
  65.     
  66.     if {[llength $showResults] > 1} {
  67.         for {set i 0} {$i < [llength $scriptName]} {incr i} {
  68.             lappend MATLeventQue "{[lindex $scriptName $i]} [lindex $showResults $i] [lindex $replyHandler $i]"
  69.         }
  70.     } else {
  71.         lappend MATLeventQue "{$scriptName} $showResults $replyHandler"
  72.     }
  73.     
  74.     if {! $matlabBusy} {
  75.         MATL::queNextEvent
  76.     }
  77. }
  78.  
  79. ################################################################################
  80. # Switch to Matlab
  81. ################################################################################
  82.  
  83. proc matlab {} {
  84.     global matlabSig
  85.     app::launchFore $matlabSig
  86. }
  87.  
  88. ################################################################################
  89. # Check if Matlab is running
  90. ################################################################################
  91.  
  92. proc isMatlabRunning {} {
  93.     global matlabSig
  94.     
  95.     return [app::isRunning $matlabSig]
  96.  
  97. #     foreach proc [processes] {
  98. #         if {[lsearch -exact $matlabSig [lindex $proc 1]] >= 0} {return 1}
  99. #     }
  100. #     return 0
  101.  
  102. }
  103.  
  104. ################################################################################ 
  105. # Matlab Save Hook to clear the current function from MATLAB's memory.  
  106. # This is useful for editing callback functions that don't get checked 
  107. # for modifiction.
  108. ################################################################################
  109.  
  110. proc MATL::saveHook {name} {
  111.     global MATLmodeVars
  112.  
  113.     #
  114.     # Check if the user wants functions cleared on save and sure MATLAB 
  115.     # is running.  We don't need to clear if it is not!
  116.     #
  117.     if {[isMatlabRunning] && $MATLmodeVars(clearOnSave)} then {
  118.         #
  119.         # Now clear the function
  120.         #
  121.         set procName [file rootname [file tail $name]]
  122. #         set scriptName "clear $procName"
  123.         set scriptName "clear functions"
  124.         matlabDoScript $scriptName 0
  125.         message $scriptName
  126.         return
  127.     }
  128. }
  129.  
  130.  
  131. ################################################################################
  132. #  Ask MATLAB for the location of a file.
  133. ################################################################################
  134.  
  135. proc matlabWhichFile {name} {
  136.     global matlabBusy
  137.     
  138.     if {$matlabBusy} {
  139.         alertnote "Matlab is busy, try again later."
  140.         return ""
  141.     }
  142.     
  143.     set mFile [string trim [matlabDoScript "which $name" 0 1]]
  144.     
  145.     # if it is a mex-file get the associated m-file
  146.     if {[string compare [file extension $mFile] ".mex"] == 0} {
  147.         set fRoot [file rootname $mFile]
  148.         set mExt {.m}
  149.         set mFile $fRoot$mExt
  150.     }
  151.     
  152.     return $mFile
  153. }
  154.  
  155. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
  156. #
  157. # ◊◊◊◊ Send stuff to MATLAB ◊◊◊◊ #
  158. #
  159. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-
  160.  
  161. ################################################################################
  162. #  Edit current window in MATLAB
  163. ################################################################################
  164.  
  165. proc editInMatlab {} {
  166.     
  167.     if {[winDirty]} {
  168.         if {[askyesno "Save '[lindex [winNames] 0]'?"] == "yes"} {
  169.             save
  170.         } else {
  171.             return
  172.         }
  173.     }
  174.     
  175.     set matName [matlabLaunch]
  176.     
  177.     if {[catch {sendOpenEvent -n $matName [lindex [winNames -f] 0]}] } {
  178.         beep 
  179.     } else {
  180.         switchTo $matName
  181.         killWindow
  182.     }
  183. }
  184.  
  185.  
  186. ################################################################################
  187. #  Debug current window in MATLAB
  188. ################################################################################
  189.  
  190. proc debugInMatlab {} {
  191.     
  192.     if {[winDirty]} {
  193.         if {[askyesno "Save '[lindex [winNames] 0]'?"] == "yes"} {
  194.             save
  195.         } else {
  196.             return
  197.         }
  198.     }
  199.     
  200.     set mFile [lindex [winNames -f] 0]
  201.     set procName [file rootname [file tail $mFile]]
  202.     
  203.     set scriptName "mactools mdebug $procName"
  204.     matlabDoScript $scriptName 0
  205.     switchTo [matlabLaunch]
  206. }
  207.  
  208.  
  209. ################################################################################
  210. #  Save current window and execute it in MATLAB
  211. ################################################################################
  212.  
  213. proc matlabSaveAndExecute {} {
  214.     global MATLmodeVars
  215.     
  216.     # Temporarily turn off clearOnSave so we can send all events at once
  217.     if {[winDirty]} {
  218.         set oldClearOnSave $MATLmodeVars(clearOnSave)
  219.         set MATLmodeVars(clearOnSave) 0
  220.         save
  221.         set MATLmodeVars(clearOnSave) $oldClearOnSave
  222.     }
  223.     
  224.     # Get the path of the current window and it's name
  225.     set mFile [lindex [winNames -f] 0]
  226.     regsub -all -- {'} [file dirname $mFile] {''} mFilePath
  227.     set procName [file rootname [file tail $mFile]]
  228.     
  229.     # Begin building event list
  230.     # Change current working directory to window's and do the script
  231.     set theScripts [list "cd '$mFilePath'" "$procName"]
  232.     set handlers [list MATL::NullReplyHandler MATL::ReplyHandler]
  233.     set showResults [list 0 1]
  234.     
  235.     # Now add the clearOnSave if necessary to event list
  236.     if {$MATLmodeVars(clearOnSave) && [isMatlabRunning]} then {
  237. #         set scriptName "clear $procName"
  238.         set scriptName "clear functions"
  239.         set theScripts [concat [list $scriptName] $theScripts]
  240.         set handlers [concat [list MATL::NullReplyHandler] $handlers]
  241.         set showResults [concat [list 0] $showResults]
  242.     }
  243.     
  244.     # Do the script
  245.     matlabDoScript  $theScripts $showResults 0 $handlers
  246. }
  247.  
  248.  
  249. ################################################################################
  250. #  Send line to MATLAB
  251. ################################################################################
  252.  
  253. proc matlabDoLine {} {
  254.     beginningOfLine
  255.     set bol [getPos]
  256.     endOfLine
  257.     set eol [getPos]
  258.     set scriptName [getText $bol $eol]
  259.     matlabDoScript $scriptName
  260. }
  261.  
  262.  
  263. ################################################################################
  264. #  Send line or selection to MATLAB
  265. ################################################################################
  266.  
  267. proc matlabDoSelectionOrLine {} {
  268.     if {[pos::compare [getPos] == [selEnd]]} {
  269.     set bol [lineStart [getPos]]
  270.     set eol [pos::math [nextLineStart [getPos]] - 2]
  271.     set scriptName [string trim [getText $bol $eol]]
  272.     } else {
  273.     set scriptName [string trim [getSelect]]
  274.     }
  275.     
  276.     matlabDoScript $scriptName
  277.     return $scriptName
  278. }
  279.  
  280.  
  281. ################################################################################
  282. #  Open selected .m file
  283. ################################################################################
  284.  
  285. proc matlabOpenSelection {} {
  286.     MATL::editMfile [getSelect]
  287. }
  288.  
  289. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
  290. #
  291. # ◊◊◊◊ Reply handlers ◊◊◊◊ #
  292. #
  293. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-
  294.  
  295.  
  296. ################################################################################
  297. #  Handle the reply from MATLAB
  298. ################################################################################
  299.  
  300. proc MATL::ReplyHandler {res} {
  301.     
  302.     if {[regexp -- {(“)([^”]*)} $res dum1 dum2 res1]} {
  303.         matlabResults $res1
  304.     } else {
  305.         alertnote $res1
  306.     }
  307.     
  308.     MATL::queNextEvent
  309.     
  310.     return 1
  311. }
  312.  
  313.  
  314. ################################################################################
  315. #  Handle the reply from MATLAB by doing nothing special
  316. ################################################################################
  317.  
  318. proc MATL::NullReplyHandler {res} {
  319.     MATL::queNextEvent
  320.     return 1
  321. }
  322.  
  323.  
  324.  
  325. ################################################################################
  326. #  Handle the reply from MATLAB for a definition
  327. ################################################################################
  328.  
  329. proc MATL::DocReplyHandler {res} {
  330.     global MATLcurrentScript
  331.     
  332.     if {[regexp -- {(“)([^”]*)} $res dum1 dum2 res1]} {
  333.         if {[regexp -- {not found.} [string range $res 0 100]]} {
  334.             alertnote $res1
  335.         } else {
  336.             matlabShowDefinition $MATLcurrentScript $res1
  337.         }
  338.     } else {
  339.         alertnote $res
  340.     }
  341.     
  342.     MATL::queNextEvent
  343.     
  344.     return 1
  345. }
  346.         
  347. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
  348. #
  349. # ◊◊◊◊ Event que ◊◊◊◊ #
  350. #
  351. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-
  352.  
  353. ################################################################################
  354. #  Move the next event up in the que
  355. ################################################################################
  356.  
  357. proc MATL::queNextEvent {} {
  358.     global MATLeventQue matlabBusy
  359.     
  360.     set matlabBusy 0
  361.     
  362.     if {[llength $MATLeventQue] > 0} {
  363.         set nextEvent [lindex $MATLeventQue 0]
  364.         set MATLeventQue [lrange $MATLeventQue 1 end]
  365.         eval "MATL::DoEvent $nextEvent"
  366.     }
  367. }
  368.  
  369.  
  370. ################################################################################
  371. #  Do the current event
  372. ################################################################################
  373.  
  374. proc MATL::DoEvent {scriptName showResults replyHandler} {
  375.     global matlabBusy MATLcurrentScript
  376.     
  377.     switch -- $showResults {
  378.         0 {
  379.             currentReplyHandler MATL::NullReplyHandler
  380.             dosc -q -c {'MATL'} -s $scriptName
  381.         }
  382.         1 {
  383.             matlabCmdWindow
  384.             insertText "${scriptName}\n"
  385.             set MATLcurrentScript $scriptName
  386.             currentReplyHandler $replyHandler
  387.             dosc -q -c {'MATL'} -s $scriptName
  388.         }
  389.         2 {
  390.             set MATLcurrentScript $scriptName
  391.             currentReplyHandler $replyHandler
  392.             dosc -q -c {'MATL'} -s $scriptName
  393.         }
  394.     }
  395.     set matlabBusy 1
  396. }
  397.  
  398. ################################################################################
  399. #  Manually clear the event que
  400. ################################################################################
  401.  
  402. proc MATL::clearEventQue {} {
  403.     global MATLeventQue matlabBusy
  404.     
  405.     set matlabBusy 0
  406.     set MATLeventQue ""
  407.     
  408. }
  409.  
  410.  
  411.